package com.knowledge.business.controller;


import java.util.Arrays;
import java.util.List;
import java.io.IOException;

import com.knowledge.common.annotation.Log;
import com.knowledge.common.core.controller.BaseController;
import com.knowledge.common.core.domain.AjaxResult;
import com.knowledge.common.enums.BusinessType;
import com.knowledge.common.utils.poi.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.knowledge.business.domain.SysDevice;
import com.knowledge.business.service.ISysDeviceService;
import com.knowledge.common.core.page.TableDataInfo;

/**
 * 设备信息Controller
 *
 * @author zhaoyk
 * @date 2025-09-23
 */
@RestController
@RequestMapping("/business/device")
public class SysDeviceController extends BaseController {
    @Autowired
    private ISysDeviceService sysDeviceService;

    /**
     * 查询设备信息列表
     */
//@RequiresPermissions("business:device:list")
    @GetMapping("/list")
    public TableDataInfo list(SysDevice sysDevice) {
        startPage();
        List<SysDevice> list = sysDeviceService.selectSysDeviceList(sysDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备信息列表
     */
    //@RequiresPermissions("business:device:export")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDevice sysDevice) {
        List<SysDevice> list = sysDeviceService.selectSysDeviceList(sysDevice);
        ExcelUtil<SysDevice> util = new ExcelUtil<SysDevice>(SysDevice.class);
        util.exportExcel(response, list, "设备信息数据");
    }

    /**
     * 获取设备信息详细信息
     */
    //@RequiresPermissions("business:device:query")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") String deviceId) {
        return AjaxResult.success(sysDeviceService.selectSysDeviceByDeviceId(deviceId));
    }

    /**
     * 新增设备信息
     */
    //@RequiresPermissions("business:device:add")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDevice sysDevice) {
        return toAjax(sysDeviceService.insertSysDevice(sysDevice));
    }

    /**
     * 修改设备信息
     */
    //@RequiresPermissions("business:device:edit")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDevice sysDevice) {
        String deviceId = sysDevice.getDeviceId();
        SysDevice sysDevice1 = sysDeviceService.selectSysDeviceByDeviceId(deviceId);
        if (sysDevice1 == null) {
            sysDeviceService.insertSysDevice(sysDevice);
        } else {
            sysDeviceService.updateSysDevice(sysDevice);
        }
        return AjaxResult.success();
    }

    /**
     * 删除设备信息
     */
    //@RequiresPermissions("business:device:remove")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable String[] deviceIds) {
        return toAjax(sysDeviceService.deleteSysDeviceByDeviceIds(deviceIds));
    }
}
